package com.ltu.controller.customize;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.core.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ltu.model.request.base.BaseFilterPageReq;
import com.ltu.model.request.base.BaseIdReq;
import com.ltu.model.response.base.CodeDataResp;
import com.ltu.model.response.order.OrderVO;
import com.ltu.service.impl.BaseServiceImpl;
import com.ltu.util.common.MpPageUtil;
import com.ltu.vo.XcxUserOrderCountVO;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.ltu.config.shiro.ShiroUtil;
import com.ltu.config.shiro.dto.UserDto;
import com.ltu.domain.mp_entity.OrderEntity;
import com.ltu.domain.mp_entity.customize.ServiceOrderEntity;
import com.ltu.mapper.customize.ServiceOrderMapper;


/**
 * <p>
 * 服务订单 前端控制器
 * </p>
 *
 * @author 若尘
 * @since 2024-09-26
 */
@RestController

@RequestMapping("/ServiceOrder")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(tags= "服务订单")
public class ServiceOrderController extends BaseServiceImpl<ServiceOrderMapper, ServiceOrderEntity>{
	
	private QueryWrapper<ServiceOrderEntity>  getCondition(BaseFilterPageReq<ServiceOrderEntity> req) {
		UserDto uDto = ShiroUtil.getPrincipalUser();
		QueryWrapper<ServiceOrderEntity> t = new QueryWrapper<ServiceOrderEntity>();
		if (StringUtils.isNotBlank(req.getStartTime()))
			t.ge("o.create_time", req.getStartTime());
		if (StringUtils.isNotBlank(req.getEndTime()))
			t.le("o.create_time", req.getEndTime());
//		t.eq("o.pay_status", 1);
		if (req.getEntityParam()!=null ) {
			if(req.getEntityParam().getUserId()!=null)
				t.eq("o.user_id ", req.getEntityParam().getUserId());
			if(StringUtils.isNotBlank(req.getEntityParam().getOrderNo()))
				t.eq( "o.order_no", req.getEntityParam().getOrderNo());
			if(req.getEntityParam().getStatus()!=null )
				t.eq( "o.status", req.getEntityParam().getStatus());
			
			if(req.getEntityParam().getRefundFlag()!=null )
				t.gt( "o.refund_flag", 0);
		}
		t.ne("o.status ", 0);
		
		if(Assert.isNonEmpty(req.getIncludeIds())) 
			t.in("o.status",req.getIncludeIds());
		
		if (null != req.getKeyWords() && req.getKeyWords().size() > 0) {
//			if (req.getKeyWords().containsKey("serviceTitle")) {
//				JSONObject  j = new JSONObject();
//				j.put("serviceTitle", req.getKeyWords().getString("serviceTitle"));
//				t.apply(getLikeStr(j, "o."));
//				req.getKeyWords().remove("serviceTitle");
//			}
			if(req.getKeyWords().size()>0)
				t.apply(getLikeStr(req.getKeyWords(), "o."));			
		}
		
		
		 
		
		return t;
	}
	
    @ApiOperation(value="获取分页列表")
    @RequestMapping(value="/getMePage", method= RequestMethod.POST)
    public CodeDataResp<Page<ServiceOrderEntity>> getMePage(@RequestBody  BaseFilterPageReq<ServiceOrderEntity> req ) {
		UserDto uDto = ShiroUtil.getPrincipalUser();

		Page<ServiceOrderEntity> page = MpPageUtil.getCommonPage(req.getPageReq());
		 
		QueryWrapper<ServiceOrderEntity> t =	getCondition(req);
		
		Page<ServiceOrderEntity> result = this.getBaseMapper().customisePage(page	, t);
		return CodeDataResp.valueOfSuccess(result);
    }
    
    @ApiOperation(value="获取列表")
    @RequestMapping(value="/getList", method= RequestMethod.POST)
    public CodeDataResp<List<ServiceOrderEntity>> getList(@RequestBody  BaseFilterPageReq<ServiceOrderEntity> req ) {
        return super.getMeList(req);
    }

    @ApiOperation(value="新增/保存")
    @RequestMapping(value="/edit", method= RequestMethod.POST)
    public CodeDataResp edit(@RequestBody ServiceOrderEntity req) {
        return super.edit(req);
    }
    
//    @ApiOperation(value="批量修改数据")
//    @RequestMapping(value="/edits", method= RequestMethod.POST)
    public CodeDataResp edits(@RequestBody List<ServiceOrderEntity> reqs) {
    	super.saveOrUpdateBatch(reqs);
    	return   CodeDataResp.valueOfSuccessEmptyData();
    }
    
    
    	@ApiOperation(value="获取详情")
    @RequestMapping(value="/getDetail/{id}", method= RequestMethod.GET)
    public CodeDataResp<ServiceOrderEntity> getDetail(@PathVariable String id) {
 		BaseIdReq req=new BaseIdReq(id);
        return super.getDetail(req);
    }
    

 	 @ApiOperation(value="删除")
     @RequestMapping(value="/remove/{id}", method= RequestMethod.GET)
     public CodeDataResp remove(@PathVariable String id) {
     	BaseIdReq req=new BaseIdReq(id);
     	return super.remove(req);
     }
     
 //   @ApiOperation(value="删除s")
//    @RequestMapping(value="/removes", method= RequestMethod.POST)
    public CodeDataResp removes(@RequestBody List<Integer> ids) {
    		super.removeByIds(ids);
        return CodeDataResp.valueOfSuccessEmptyData();
    }

}

